//
//  WPDetailCommentVC.m
//  wobingwoyi
//
//  Created by WGP on 2016/11/25.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPDetailCommentVC.h"
#import "WPNewCommentModel.h"
#import "WPNewCommentCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

@interface WPDetailCommentVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIView *navBarView;

@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UIButton *praiseBtn;
@property(nonatomic,strong)UILabel *praiseCount;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UILabel *timeLabel;

@property(nonatomic,strong)UITableView *bottomTableView;
@property(nonatomic,strong)NSMutableArray *models;
@property(nonatomic,assign)int indexPage;

@end

@implementation WPDetailCommentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];

}

-(void)setupUI
{
    [self navBarView];
    [self setupMainComment];
    [self bottomTableView];
}

-(UIView *)navBarView
{
    if (_navBarView == nil)
    {
        _navBarView = [UIView new];
        [self.view addSubview:_navBarView];
        
        [_navBarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(20);
            make.height.mas_equalTo(44);
        }];
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = @"详情";
        titleLabel.font = [UIFont systemFontOfSize:18];
        titleLabel.textColor = GLobalFontColor;
        [_navBarView addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_navBarView);
        }];
        
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
        [backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_navBarView addSubview:backBtn];
        
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_navBarView);
            make.left.equalTo(self.navBarView).offset(15);
        }];
        
        UIImageView *lineView = [UIImageView lineImageView];
        [_navBarView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_navBarView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _navBarView;
}

-(void)setupMainComment
{
    [self iconView];
    [self nameLabel];
    [self contentLabel];
    [self timeLabel];
    [self praiseBtn];
    [self praiseCount];
}

-(UIImageView *)iconView
{
    if (_iconView == nil)
    {
        UIImage *image = [UIImage imageNamed:@"comment-default_icon"];
        _iconView = [[UIImageView alloc] initWithImage:image];
        
        _iconView.layer.cornerRadius = image.size.width*0.5;
        _iconView.layer.masksToBounds = YES;
        
        [self.view addSubview:_iconView];
        
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(15);
            make.top.equalTo(self.navBarView.mas_bottom).offset(20);
            make.width.height.mas_equalTo(image.size.width);
        }];
    }
    return _iconView;
}

-(UILabel *)nameLabel
{
    if (_nameLabel == nil)
    {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = RGB_SAME(102);
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.text = self.model.userName;
        [_nameLabel sizeToFit];
        [self.view addSubview:_nameLabel];
        
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconView);
            make.left.equalTo(self.iconView.mas_right).offset(15);
        }];
    }
    return _nameLabel;
}

-(UILabel *)contentLabel
{
    if (_contentLabel == nil)
    {
        _contentLabel = [UILabel new];
        _contentLabel.numberOfLines = 0;
        _contentLabel.text = self.model.content;
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = GLobalFontColor;
        [self.view addSubview:_contentLabel];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel);
            make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
            make.right.equalTo(self.view).offset(-15);
        }];
    }
    return _contentLabel;
}

-(UILabel *)timeLabel
{
    if (_timeLabel == nil)
    {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = RGB_SAME(102);
        [_timeLabel sizeToFit];
        _timeLabel.text = [[WPDateFormatterManager sharedManager] wbwyTimeStrDescriptionWithTimeStampStr:[NSString stringWithFormat:@"%zd",self.model.time]];
        [self.view addSubview:_timeLabel];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentLabel);
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
            
        }];
        
    }
    return _timeLabel;
}

-(UILabel *)praiseCount
{
    if (_praiseCount == nil)
    {
        _praiseCount = [UILabel new];
        _praiseCount.textColor = RGB_SAME(183);
        _praiseCount.font = [UIFont systemFontOfSize:14];
        _praiseCount.text = [NSString stringWithFormat:@"%zd",self.model.praiseAmount];
        _praiseCount.textAlignment = NSTextAlignmentRight;
        [self.view addSubview:_praiseCount];
        
        [_praiseCount mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.right.equalTo(self.view).offset(-15);
        }];
    }
    return _praiseCount;
}

-(UIButton *)praiseBtn
{
    if (_praiseBtn == nil)
    {
        _praiseBtn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"reading_praise_normal"] forState:UIControlStateNormal];
        [_praiseBtn setBackgroundImage:[UIImage imageNamed:@"reading_praise_selected"] forState:UIControlStateSelected];
        _praiseBtn.selected = self.model.isPraised;
        [_praiseBtn addTarget:self action:@selector(praiseBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_praiseBtn];
        
        [_praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.timeLabel);
            make.right.equalTo(self.praiseCount.mas_left).offset(-5);
            
        }];
    }
    return _praiseBtn;
}

-(UITableView *)bottomTableView
{
    if (_bottomTableView == nil)
    {
        _bottomTableView = [[UITableView alloc] init];
        _bottomTableView.backgroundColor = RGB_SAME(245);
        
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        headerView.backgroundColor = GlobalBgColor;
        _bottomTableView.tableHeaderView = headerView;
        
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = @"全部评论";
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textColor = GLobalFontColor;
        [headerView addSubview:titleLabel];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView).offset(15);
            make.left.equalTo(headerView).offset(15);
        }];
        
        _bottomTableView.delegate = self;
        _bottomTableView.dataSource = self;
        _bottomTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_bottomTableView registerClass:[WPNewCommentCell class] forCellReuseIdentifier:@"WPNewCommentCell"];
        [self.view addSubview:_bottomTableView];
        
        [_bottomTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.timeLabel.mas_bottom).offset(15);
        }];
    }
    return _bottomTableView;
}

-(void)setModel:(WPNewCommentModel *)model
{
    _model = model;
    
    NSString *url = @"appraise/findAppraise.do";
    NSDictionary *dict = @{@"recordId":@(self.recordId),@"id":@(model.id),@"page":@(0)};
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"子评论:%@",responseObject);
        NSDictionary *responseDict = (NSDictionary *)responseObject;
        if ([responseDict[@"result"] isEqualToString:@"success"])
        {
            NSArray *arr = responseDict[@"detail"];
            
            //有评论的时候
            if (arr.count)
            {
                //            不满一页影藏底部刷新控件
                if (arr.count < 10)
                {
                    self.bottomTableView.mj_footer.hidden = YES;
                }
                
                for (NSDictionary *dict in arr)
                {
                    WPNewCommentModel *model = [WPNewCommentModel yy_modelWithDictionary:dict];
                    [self.models addObject:model];
                }
                
                //评论页数加1
                self.indexPage++;
                
                [self.bottomTableView.mj_footer endRefreshing];
                [self.bottomTableView reloadData];
            }
        }
        else
        {
            //            考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //                账号过期
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
            }
            else
            {
                [self showErrorInfoWithStatus:@"加载失败"];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
}

-(void)praiseBtnDidClick:(UIButton *)btn
{
    
}

#pragma mark - tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPNewCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPNewCommentCell"];
    cell.isSubAppraise = YES;
    cell.model = self.models[indexPath.item];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:@"WPNewCommentCell" cacheByIndexPath:indexPath configuration:^(WPNewCommentCell *cell) {
        // 配置 cell 的数据源，和 "cellForRow" 干的事一致，比如：
        cell.model = self.models[indexPath.row];
    }];
}

-(NSMutableArray *)models
{
    if (_models == nil)
    {
        _models = [NSMutableArray array];
    }
    return _models;
}

-(void)backBtnDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
